package cn.lh.event;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

import java.util.concurrent.TimeUnit;

/**
 * @描述:
 * @版权: Copyright (c) 2023
 * @作者: linhong
 * @版本: 1.0.0
 * @创建时间: 2023/10/12
 */
@Configuration
public class MyListenerAnnotation2 {

    @EventListener
    public AnotherEvent onApplicationEvent(MyEvent event){
        System.out.println("监听器1 接收到事件: " + JSONObject.toJSONString(event));
        return new AnotherEvent(this,"转发"+event.getMessage());
    }

    @EventListener
    @Async
    public void onApplicationEvent1(AnotherEvent event){
        System.out.println("监听器2 接收到事件: " + JSONObject.toJSONString(event));
        // 模拟事件处理
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("监听器2结束 " );
    }
}
